"""
dataclass 对象
    field 修饰的对象, 是独立的对象, 不会相互影响

"""

from dataclasses import dataclass, field


@dataclass
class Body:
    wight: int = 0
    height: int = 0


@dataclass
class Student:
    id: int = 0
    name: str = ""
    body: Body = Body()  # 默认共用一个对象
    body2: Body = field(default_factory=Body)  # fidld修饰, 独立对象, 不会相互影响


def test1_no_field():
    tom = Student(1, "tom")
    tom.body.wight = 100
    tom.body.height = 180
    jack = Student(2, "jack")
    jack.body.wight = 90
    jack.body.height = 170
    print(f"tom wight: {tom.body.wight}, height: {tom.body.height}")
    print(f"jack wight: {jack.body.wight}, height: {jack.body.height}")
    print(f"tom id:{id(tom.body)}, jack id:{id(jack.body)}")


""" 
tom wight: 90, height: 170
jack wight: 90, height: 170
tom id:136189864156544, jack id:136189864156544
"""
# 结论: body: Body = Body() 共用一个对象, 修改一个, 另一个也会改变


def test2_has_field():
    tom = Student(1, "tom")
    tom.body2.wight = 100
    tom.body2.height = 180
    jack = Student(2, "jack")
    jack.body2.wight = 90
    jack.body2.height = 170
    print(f"tom wight: {tom.body2.wight}, height: {tom.body2.height}")
    print(f"jack wight: {jack.body2.wight}, height: {jack.body2.height}")
    print(f"tom id:{id(tom.body2)}, jack id:{id(jack.body2)}")


""" 
tom wight: 100, height: 180
jack wight: 90, height: 170
tom id:136189864154864, jack id:136189864152272
"""
# 结论: fidld修饰的对象是独立对象, 不会相互影响

if __name__ == "__main__":
    test1_no_field()
    test2_has_field()
